package com.zch.user.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zch.user.dao.UserDao;
import com.zch.user.entity.User;
import com.zch.user.util.DBUtil;
import com.zch.user.util.PasswordLock;

public class UserDaoImpl extends DBUtil implements UserDao {

	@Override
	public List<User> getAllUserByPage(String sql, Object[] params) {
		List<User> list = new ArrayList<>();
		executeQuery(sql, params);
		try {
			while(rs.next()){
				int uid = rs.getInt(1);
				String uName = rs.getString(2);
				String password = rs.getString(3);
				String rName = rs.getString(4);
				String tel = rs.getString(5);
				User user = new User(uid, uName, password, rName, tel);
				list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeSource();
		}
		return list;
	}

	@Override
	public int getTotalCounts(String sql, Object[] params) {
		int counts = 0;
		executeQuery(sql, params);
		try {
			while(rs.next()){
				counts = rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeSource();
		}
		return counts;
	}

	@Override
	public boolean addUser(User user) {
		String sql = "insert into user(uName,password,rName,tel) values(?,?,?,?)";
		Object[] params = new Object[]{user.getuName(),PasswordLock.Md5Lock1(user.getPassword()),user.getrName(),user.getTel()};
		executeUpdate(sql, params);
		if(count > 0){
			return true;
		}
		return false;
	}

	@Override
	public boolean deleteUser(int uId) {
		String sql = "delete from user where uId=?";
		Object[] params = new Object[]{uId};
		executeUpdate(sql, params);
		if(count > 0){
			return true;
		}
		return false;
	}

	@Override
	public boolean updateUser(User user) {
		String sql = "update user set uName=?,password=?,rName=?,tel=? where uId=?";
		Object[] params = new Object[]{user.getuName(),PasswordLock.Md5Lock1(user.getPassword()),user.getrName(),user.getTel(),user.getuId()};
		executeUpdate(sql, params);
		if(count > 0){
			return true;
		}
		return false;
	}

	@Override
	public List<User> getAllUser() {
		String sql = "select * from user";
		Object[] params = new Object[]{};
		List<User> list = new ArrayList<>();
		executeQuery(sql, params);
		try {
			while(rs.next()){
				int uid = rs.getInt(1);
				String uName = rs.getString(2);
				String password = rs.getString(3);
				String rName = rs.getString(4);
				String tel = rs.getString(5);
				User user = new User(uid, uName, password, rName, tel);
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeSource();
		}
		return list;
	}

	@Override
	public User getUser(int uId) {
		User user = null;
		String sql = "select * from user where uId=?";
		Object[] params = new Object[]{uId};
		executeQuery(sql, params);
		try {
			while(rs.next()){
				int uid = rs.getInt(1);
				String uName = rs.getString(2);
				String password = rs.getString(3);
				String rName = rs.getString(4);
				String tel = rs.getString(5);
				user = new User(uid, uName, password, rName, tel);
				}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeSource();
		}
		return user;
	}

}
